package com.jzqf.harmonydemo.utils.file;

import com.jzqf.harmonydemo.utils.LogUtil;

import java.io.File;
import java.io.FileOutputStream;

/**
 * 文件工具类
 *
 * @author LiuWeiHao
 * 2021/5/20 17:09
 */
public class FileUtil {
    /**
     * 删除文件夹下的所有文件
     *
     * @param parentPath 目录文件路径
     */
    public void deleteAll(String parentPath) {
        File file = new File(parentPath);
        file.delete();
    }

    public static File saveFile(byte[] bytes, String absolutePath) {
        try {
            File file = new File(absolutePath);
            if (file.exists()) {
                file.delete();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(bytes);
            LogUtil.d("saveFile", "下载完成了");
            fileOutputStream.flush();
            fileOutputStream.close();
            return file;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
